__author__ = 'zoulida'

import datetime
from functools import lru_cache
from sdufe.tools.santai3.tools.shelveTool import shelve_me

def getPro():
    import tushare as ts
    pro = ts.pro_api('69d6b836725cd75df21b39873603b14fed58d101bc033b991b51eb41')
    return pro

@lru_cache()
def getCal_dates():#获取交易日历
    pro = getPro()

    cal_dates2 = pro.trade_cal(exchange='')
    #cal_dates2 = pro.query('trade_cal')
    #print(cal_dates2)

    return cal_dates2

@lru_cache()
@shelve_me
def getCalforQuantAxis_dates(year):#year主要是给shelve参数
    import pandas as pd
    cal_dates = getCal_dates()
    rslt_df = cal_dates[cal_dates['is_open'] == 1]
    rslt_df['cal_date'] = pd.to_datetime(rslt_df['cal_date'])#strftime('%Y-%m-%d')
    rslt_df['cal_date'] = rslt_df['cal_date'].apply(lambda x:datetime.datetime.strftime(x,'%Y-%m-%d'))

    #print(rslt_df)
    listdays = rslt_df['cal_date']
    #print(listdays.tolist())
    return listdays.tolist()

def is_open_day(date):
    # print(cal_dates[cal_dates['calendarDate'] == date])
    cal_dates = getCal_dates()
    if date in cal_dates['cal_date'].values:
        return cal_dates[cal_dates['cal_date'] == date].iat[0, 2] == 1
    return False

def chuquan():
    pro = getPro()
    df = pro.adj_factor(ts_code='', trade_date='20190718')
    df = pro.adj_factor(ts_code='000001.SZ', trade_date='')
    print(df)
if __name__ == '__main__':
    #bl = is_open_day('20200308')
    #print(bl)

    #chuquan()
    #print(getCal_dates())
    year = datetime.datetime.now().year.__str__()
    print(getCalforQuantAxis_dates(year))